package com.martin.lib_base.utils

import android.app.Activity
import java.util.Stack


/**
 * 通过对战管理器实现多级页面的直接返回
 */
object FishStackManager {
    private var mActivityStack: Stack<Activity>? = null

    /**
     * 跳转到activity
     */
    fun pushActivity(activity: Activity?) {
        if (mActivityStack == null) {
            mActivityStack = Stack<Activity>()
        }
        mActivityStack!!.add(activity)
    }

    /**
     * 销毁一个activity
     */
    fun popActivity(activity: Activity?) {
        if (activity != null) {
            activity.finish()
            mActivityStack?.remove(activity)
        }
    }

    /**
     * 获取当前的activity
     */
    fun currentActivity(): Activity? {
        if (mActivityStack == null || mActivityStack!!.size == 0) {
            return null
        }
        val activity = mActivityStack!!.lastElement() as Activity
        return activity
    }

    /**
     * 通过clsss类名返回到相应的Activity
     * @clsss : Activity类名
     */
    fun popTopActivities(clsss: Class<*>) {
        while (true) {
            val activity = currentActivity() ?: break
            if (activity.javaClass == clsss) {
                break
            }
            popActivity(activity)
        }
    }

    /**
     * 销毁所有的Activity
     */
    fun popAllActivities() {
        while (true) {
            val activity = currentActivity() ?: break
            popActivity(activity)
        }
    }
}